package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * Create by lisy on Qingdao
 **/
@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {

    private Logger logger = LoggerFactory.getLogger(Category.class);

    @Autowired
    CategoryMapper categoryMapper;

    //增加品类
    public ServerResponse addCategoryService(String categoryName, int parentId){
        if(StringUtils.isBlank(categoryName) || parentId == 0){
            return ServerResponse.createByError();
        }
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);

        if(categoryMapper.insertSelective(category) > 0){
            return ServerResponse.createBySuccessMessage("添加成功");
        }
        return ServerResponse.createByErrorMssage("添加品类失败");
    }

    //修改名字
    public ServerResponse setCategoryNameService(int categoryId, String categoryName){
        if(categoryId == 0 || StringUtils.isBlank(categoryName)){
            return ServerResponse.createByErrorMssage("修改品类名称时参数错误");
        }
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);
        int count = categoryMapper.updateByPrimaryKeySelective(category);
        if(count > 0){
            return ServerResponse.createBySuccessMessage("成功修改品类名称");
        }
        return ServerResponse.createByErrorMssage("修改品类名称失败了");
    }

    //获取品类子节点  平级
    public ServerResponse<List<Category>> getChildrenParallelCategoryService(int categoryId){
        if(categoryId < 0){
            return ServerResponse.createByErrorMssage("取品类子节点时参数错误");
        }
        List<Category> categoryList = categoryMapper.selectChildrenParallelCategory(categoryId);
        if(categoryList.size() == 0){
            logger.info("未找到该品类的子分类");
        }
        return ServerResponse.createBySuccess("子节点如下", categoryList);
    }

    /**
     * 递归查询品类中的所有子节点Id，返回一个list集合
     * @param categoryId
     * @return
     */
    public ServerResponse selectCategoryAndChildrenById(Integer categoryId){
        Set<Category> categorySet = Sets.newHashSet();
        findChildCategory(categorySet, categoryId);

        List<Integer> categoryIdList = Lists.newArrayList();
        if(categoryId != null){
            for(Category categoryItem : categorySet) {
                categoryIdList.add(categoryItem.getId());
            }
        }
        return ServerResponse.createBySuccess(categoryIdList);
    }

    //递归算法，算出子节点
    private Set<Category> findChildCategory(Set<Category> categorySet, Integer categoryId){
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if(category != null){
            categorySet.add(category);
        }
        List<Category> list = categoryMapper.selectChildrenParallelCategory(categoryId);
        for(Category categoryItem : list){
            findChildCategory(categorySet, categoryItem.getId());
        }
        return categorySet;
    }
}
